﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HB.MVVM;
using System.Windows.Input;
using System.Collections.ObjectModel;
using HB.MVVM;
using HB.Base;
using System.Threading;
using System.Windows;
using PropertyChanged;
namespace Demo
{
    internal class VMMainWindow : ViewModelBase<VMMainWindow>
    {
        public VMMainWindow()
        {
            code = codes.ToArray().Select(z => z.ToString()).ToArray();

            for (int i = 0; i < 5; i++)
            {
                var y = new ModelY();
                ModelList.Add(y);
                dicy.Add(i.ToString(), y);
            }
        }
        [OnChangedMethod(nameof(hhhhh))]
        public int age { get; set; }
        public ObservableDictionary<string, double> dic { get; set; } = new ObservableDictionary<string, double>();
        public ObservableCollectionEx<ModelY> ModelList { get; set; } = new ObservableCollectionEx<ModelY>();
        public ObservableDictionary<string, ModelY> dicy { get; set; } = new ObservableDictionary<string, ModelY>();
        public string autofeq { get => GetString(); set => Set(value); }
        Random R = new Random();
        string codes = "abcdefghijklnmopqrsuvwsxyz89555222";
        string[] code { get; set; }
        public ICommand cmdChange => GetCommand(() =>
        {
            var index = R.Next(0, 26);
            dic[code[index]] = R.NextDouble();
        });

        void hhhhh()
        {

        }
        public ICommand cmdChange2 => GetCommand(() =>
        {
            Task.Run(() =>
            {
                var index = R.Next(0, 26);
                dic[code[index]] = R.NextDouble();
            });

        });
        TriggerTimer timer = new TriggerTimer();
        public ICommand cmdChangeauto => GetCommand(() =>
        {
            if (timer.IsRunning) { timer.Stop(); return; }
            var feq = int.Parse(autofeq);
            timer.Start(feq, (t, i) =>
            {

                var index = R.Next(0, 26);
                //  Dispatcher.Invoke(() =>
                //{

                dic[code[index]] = R.NextDouble();

                // });

            });
        });
        public ICommand cmdTextNotifyCollection => GetCommand(() =>
        {
            Random r = new Random();
            for (int i = 0; i < 5; i++)
            {
                ModelList[i].a = r.Next();
                ModelList[i].b = r.Next();
                ModelList[i].c = r.Next();
                ModelList[i].d = r.Next();
                ModelList[i].e = r.Next();
            }

            ModelList.NotifyItemChanged(2);
            dicy.NotifyItemChanged("2");
        });
    }
    class ModelY
    {
        public int a { get; set; }
        public int b { get; set; }
        public int c { get; set; }
        public int d { get; set; }
        public int e { get; set; }
    }
}
